/*
 * @Author: 王飞 
 * @Date: 2020-11-30 10:46:01 
 * @Last Modified by: 王飞
 * @Last Modified time: 2020-11-30 21:15:32
 */
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="11">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="userList" stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="电话" prop="mobile"></el-table-column>
        <el-table-column label="角色" prop="role_name"></el-table-column>
        <el-table-column label="状态">
          <!-- 利用vue的插槽拿到数据 -->
          <template v-slot="scope">
            <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="125px">
          <template v-slot>
            <el-button type="primary" icon="el-icon-edit" circle size="mini"></el-button>
            <el-tooltip effect="dark" content="分配角色" :enterable="false" placement="top">
              <el-button type="warning" icon="el-icon-setting" circle size="mini"></el-button>
            </el-tooltip>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div> 
</template>

<script>
export default {
  data() {
    return {
      // 获取用户列表对象
      queryInfo: {
        query: "",
        //当前页码
        pagenum: 1,
        pagesize: 5
      },
      userList: [],
      total: 0
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    getUserList() {
      this.$http.get("users", { params: this.queryInfo }).then(response => {
        var result = response.data;
        if (result.meta.status !== 200)
          return this.$message.error(result.meta.msg);
        this.userList = result.data.users;
        this.total = result.data.total;
      });
    },
    /**监听pagsize改变的事件 */
    handleSizeChange(newSize){
      this.queryInfo.pagesize = newSize;
      this.getUserList();
    },
    /**监听页码值改变的事件 */
    handleCurrentChange(newPage){
      this.queryInfo.pagenum = newPage;
      this.getUserList();
    }
  }
};
</script>

<style lang="less" scoped>
.el-pagination{
  margin-top: 15px;
}
</style>